package com.cincc.cinccimdemo.ui.conversation.sequence;

import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cincc.cinccimdemo.MyApplication;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.viewmodel.GlobalViewModel;
import com.cincc.cinccimdemo.widget.SequenceUserView;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.ImResponse;
import com.cincc.im.model.dbmodel.GroupInfo;
import com.cincc.im.model.dbmodel.SequenceInfo;

public class SequenceActivity extends IMBaseActivity implements View.OnClickListener {

    private static final String TAG = "SequenceActivity";

    @Override
    protected int contentLayout() {
        return R.layout.activity_group_sequence;
    }

    ImageView avatarImg;
    TextView titleText;
    EditText contentEdit;
    LinearLayout participantsLinearlayout;
    ImageView addImg;
    EditText remarkEdit;

    String groupId = "";
    String sequenceId = "";
    boolean isCreateSequence = false;
    GlobalViewModel sequenceViewModel;
    GroupInfo groupInfo;

    @Override
    protected void bindViews() {
        super.bindViews();
        groupId = getIntent().getStringExtra(AttrConstants.GROUP_ID);
        isCreateSequence = getIntent().getBooleanExtra(AttrConstants.IS_CREATE_SEQUENCE, false);
        sequenceId = getIntent().getStringExtra(AttrConstants.SEQUENCE_ID);
        avatarImg = findViewById(R.id.avatar_img);
        titleText = findViewById(R.id.title_text);
        contentEdit = findViewById(R.id.content_edit);
        participantsLinearlayout = findViewById(R.id.participants_ll);
        addImg = findViewById(R.id.add_img);
        remarkEdit = findViewById(R.id.remark_edit);
        addImg.setOnClickListener(this);

        MyApplication app = (MyApplication) getApplication();
        sequenceViewModel = app.getViewModel();
        if (!TextUtils.isEmpty(groupId)) {
            groupInfo = IMSipPhoneCtrl.getInstance().getGroupInfo(groupId);
        }
    }

    @Override
    protected int menu() {
        return R.menu.group_sequence;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.add_group_sequence) {
            confirm();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void afterViews() {
        super.afterViews();
//        Glide.with(this).load(Constants.userInfo.avatarUrl).placeholder(R.mipmap.avatar_def)
//                .transforms(new CenterCrop(), new RoundedCorners(15))
//                .into(avatarImg);

        if (isCreateSequence) {
            contentEdit.setText("#接龙");
            String title = "由" + Constants.userInfo.nickName + "发起接龙,参与接龙目前共0人";
            titleText.setText(title);
            SequenceUserView sequenceUserView = new SequenceUserView(this);
            sequenceUserView.setData(1, Constants.userInfo.nickName);
            participantsLinearlayout.addView(sequenceUserView);
        } else {
            if (!TextUtils.isEmpty(sequenceId)) {
                SequenceInfo groupSequence = IMSipPhoneCtrl.getInstance().getGroupSequence(sequenceId);
                if (groupSequence != null) {
                    int count = groupSequence.participantList.size();
                    String title = "由" + groupSequence.fromUserName + "发起接龙,参与接龙目前共" + count + "人";
                    titleText.setText(title);

                    contentEdit.setText(groupSequence.sequenceContent);
                    remarkEdit.setText(groupSequence.remark);

                    for (int i = 0; i < groupSequence.participantList.size(); i++) {
                        SequenceUserView sequenceUserView = new SequenceUserView(this);
                        sequenceUserView.setData(i + 1, groupSequence.participantList.get(i).nickName);
                        participantsLinearlayout.addView(sequenceUserView);
                    }
                }
            }
        }
    }

    private void confirm() {
        String content = contentEdit.getText().toString();
        String remark = remarkEdit.getText().toString();
        if (isCreateSequence) {
            IMSipPhoneCtrl.getInstance().createGroupSequence(groupId, content, remark, new ImCallback() {
                @Override
                public void callback(ImResponse imResponse) {
                    String result = imResponse.getCode() == 0 ? "创建成功" : imResponse.getMsg();

                    runOnUiThread(() -> {
                        Toast.makeText(SequenceActivity.this, result, Toast.LENGTH_SHORT).show();
                        if (imResponse.getCode() == 0) {
                            SequenceInfo sequenceInfo = (SequenceInfo) imResponse.getData();
                            Log.e(TAG, "callback: sequenceInfo.sequenceId = " + sequenceInfo.sequenceId);
                            Log.e(TAG, "callback: sequenceInfo.sequenceContent = " + sequenceInfo.sequenceContent);
                            Log.e(TAG, "callback: sequenceInfo.fromUserId = " + sequenceInfo.fromUserId);
                            Log.e(TAG, "callback: sequenceInfo.fromUserName = " + sequenceInfo.fromUserName);
                            Log.e(TAG, "callback: sequenceInfo.fromGroupId = " + sequenceInfo.fromGroupId);
                            Log.e(TAG, "callback: sequenceInfo.remark = " + sequenceInfo.remark);
                            Log.e(TAG, "callback: sequenceInfo.participants = " + sequenceInfo.participants);
                            sequenceViewModel.setSequenceInfo(sequenceInfo);
                            finish();
                        }
                    });
                }
            });
        } else {
            IMSipPhoneCtrl.getInstance().addGroupSequence(sequenceId, new ImCallback() {
                @Override
                public void callback(ImResponse imResponse) {
                    String result = imResponse.getCode() == 0 ? "参与成功" : imResponse.getMsg();
                    runOnUiThread(() -> {
                        Toast.makeText(SequenceActivity.this, result, Toast.LENGTH_SHORT).show();
                        if (imResponse.getCode() == 0) {
                            SequenceInfo sequenceInfo = (SequenceInfo) imResponse.getData();
                            Log.e(TAG, "callback: sequenceInfo.sequenceId = " + sequenceInfo.sequenceId);
                            Log.e(TAG, "callback: sequenceInfo.sequenceContent = " + sequenceInfo.sequenceContent);
                            Log.e(TAG, "callback: sequenceInfo.fromUserId = " + sequenceInfo.fromUserId);
                            Log.e(TAG, "callback: sequenceInfo.fromUserName = " + sequenceInfo.fromUserName);
                            Log.e(TAG, "callback: sequenceInfo.fromGroupId = " + sequenceInfo.fromGroupId);
                            Log.e(TAG, "callback: sequenceInfo.remark = " + sequenceInfo.remark);
                            Log.e(TAG, "callback: sequenceInfo.participants = " + sequenceInfo.participants);
                            sequenceViewModel.setSequenceInfo(sequenceInfo);
                            finish();
                        }
                    });
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        int count = participantsLinearlayout.getChildCount();
        String showName = groupInfo == null ? Constants.userInfo.nickName : groupInfo.nickNameInGroup;
        SequenceUserView sequenceUserView = new SequenceUserView(this);
        sequenceUserView.setData(count + 1, showName);
        participantsLinearlayout.addView(sequenceUserView);
    }
}
